package com.aoxin.service.impl.activity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.aoxin.mapper.activity.SlfSigninConfigMapper;
import com.aoxin.mapper.activity.SlfSigninMapper;
import com.aoxin.mapper.opm.SlfUserMapper;
import com.aoxin.model.activity.SlfSignin;
import com.aoxin.model.activity.SlfSigninConfig;
import com.aoxin.model.opm.SlfUser;
import com.aoxin.service.inter.activity.SigninService;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.enumerate.HortationYtpe;

@Service("signinService")
public class SigninServiceImpl implements SigninService {

	private final static String REMARK = "[%s]在[%s]签到获得了[%s]个[%s]";

	@Autowired
	private SlfSigninMapper slfSigninMapper;

	@Autowired
	private SlfSigninConfigMapper slfSigninConfigMapper;

	@Autowired
	private SlfUserMapper slfUserMapper;

	@Override
	public SlfSignin getSignin(Integer signinId) {
		Assert.notNull(signinId, String.format(ErrorUtil.PARAMETER_NULL, "signinId"));
		return this.slfSigninMapper.selectByPrimaryKey(signinId);
	}

	@Override
	@Transactional
	public void deleteSignins(List<Integer> signinIds) {
		Assert.notEmpty(signinIds, String.format(ErrorUtil.PARAMETER_NULL, "signinIds"));
		this.slfSigninMapper.deleteByPrimaryKeys(signinIds);
	}

	@Override
	@Transactional
	public void insertSignin(SlfSignin slfSignin) {
		Assert.notNull(slfSignin, String.format(ErrorUtil.PARAMETER_NULL, "slfSignin"));
		Date date = new Date();

		SlfSigninConfig signinConfig = this.slfSigninConfigMapper.selectByPrimaryKey(slfSignin.getSigninConfigId());

		// 签到获得立方币
		if (HortationYtpe.LFB.getId() == signinConfig.getHortationType()) {
			SlfUser user = this.slfUserMapper.selectByPrimaryKey(slfSignin.getUserId());
			user.setLfb(user.getLfb() + signinConfig.getHortation());
			this.slfUserMapper.updateByPrimaryKeySelective(user);
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		slfSignin.setRemark(String.format(REMARK, slfSignin.getUserName(), sdf.format(date), signinConfig.getHortation(), signinConfig.getRemark()));

		// 设置签到组号
		List<SlfSigninConfig> slfSigninConfigs = this.slfSigninConfigMapper.getList();
		Integer group = this.slfSigninMapper.getGroup(slfSignin.getUserId());
		Integer groupCount = this.slfSigninMapper.getCountByGroup(slfSignin.getUserId(), group);
		Integer newGroup;
		if (groupCount < slfSigninConfigs.size()) {
			newGroup = group;
		} else {
			newGroup = group + 1;
		}
		slfSignin.setGroup(newGroup);
		slfSignin.setCreateDate(date);
		this.slfSigninMapper.insertSelective(slfSignin);
	}

	@Override
	@Transactional
	public void updateSignin(SlfSignin slfSignin) {
		Assert.notNull(slfSignin, String.format(ErrorUtil.PARAMETER_NULL, "slfSignin"));
		this.slfSigninMapper.updateByPrimaryKeySelective(slfSignin);
	}

	@Override
	@Transactional
	public void updateSigninStates(Integer state, List<Integer> signinId) {
		Assert.notNull(state, String.format(ErrorUtil.PARAMETER_NULL, "state"));
		Assert.notEmpty(signinId, String.format(ErrorUtil.PARAMETER_NULL, "signinId"));
		this.slfSigninMapper.updateStates(state, signinId);
	}

	@Override
	public List<SlfSignin> getSigninList() {
		return this.slfSigninMapper.getList();
	}
}
